package com.fzfnn.scrapbook.mapper;

import com.fzfnn.scrapbook.entity.UserFollow;
import com.fzfnn.scrapbook.util.R;
import com.fzfnn.scrapbook.vo.JournalVo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserFollowMapper {

    // 添加关注
    @Insert("INSERT INTO user_follow(follower_id, followed_id) VALUES(#{followerId}, #{followedId})")
    int insertFollow(@Param("followerId") int followerId, @Param("followedId") int followedId);

    // 取消关注
    @Delete("DELETE FROM user_follow WHERE follower_id = #{followerId} AND followed_id = #{followedId}")
    int deleteFollow(@Param("followerId") int followerId, @Param("followedId") int followedId);

    // 查询是否已关注
    @Select("SELECT * FROM user_follow WHERE follower_id = #{followerId} AND followed_id = #{followedId}")
    UserFollow checkFollow(@Param("followerId") int followerId, @Param("followedId") int followedId);

    // 获取当前用户关注的用户ID列表
    @Select("SELECT followed_id FROM user_follow WHERE follower_id = #{followerId}")
    List<Integer> getFollowedIds(@Param("followerId") int followerId);
    //获取当前用户关注的用户信息
    @Select({
            "<script>",
            "SELECT u.user_id, u.username, u.imgurl, u.signature",
            "FROM user u",
            "WHERE u.user_id IN",
            "<foreach item='user_id' collection='followedIds' open='(' separator=',' close=')'>",
            "#{user_id}",
            "</foreach>",
            "</script>"
    })
    List<Map<String, Object>> getFollowedInfoByIds(@Param("followedIds") List<Integer> followedIds);

    // 获取粉丝ID列表（谁关注了我）
    @Select("SELECT follower_id FROM user_follow WHERE followed_id = #{userId}")
    R getFollowerIds(@Param("userId") int userId);
    // 获取粉丝ID列表用户详情（谁关注了我）
    @Select("SELECT u.user_id, u.username, u.imgurl,u.signature " +
            "FROM user u " +
            "JOIN user_follow f ON u.user_id = f.follower_id " +
            "WHERE f.followed_id = #{userId}")
    List<Map<String, Object>> getFollowerFanInfo(@Param("userId") int userId);

    //获取关注用户的手账
    @Select("SELECT \n" +
            "  j.journal_id AS journalId,\n" +
            "  j.title,\n" +
            "  j.content,\n" +
            "  j.img_url AS imgurl,\n" +
            "  j.tag,\n" +
            "  j.created_at AS createdAt,\n" +
            "  j.updated_at AS updatedAt,\n" +
            "  j.like_count,\n" +
            "  j.user_id AS userId,\n" +
            "  u.username AS username,\n" +
            "  u.imgurl AS avatarUrl\n" +
            "FROM user_follow uf\n" +
            "JOIN journal j ON uf.followed_id = j.user_id\n" +
            "JOIN user u ON j.user_id = u.user_id\n" +
            "WHERE uf.follower_id = #{userId}\n" +
            "  AND j.is_public = 1\n" +
            "ORDER BY j.created_at DESC;\n")
    List<JournalVo> getFollowedJournals(@Param("userId") int userId);

}
